package first;

import first.dialogs.LibDialog;
import first.liter.AbstractLanguage;
import first.liter.BaseLanguage;
import first.liter.Literature;
import first.liter.Literature.LITERATURE;
import first.nets.LibrarianNetwork;
import first.nets.ReaderNetwork;
import java.util.List;
import java.util.ArrayList;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

//import javax.annotation.Resource;

/**
 * 
 * @author Kozlov Kirill
 * @version v.0
 *
 */
public class Library {
	
//	public enum LITERATURE { BOOK, JOURNAL };
	/**
	 * Name of library
	 */
	private String nameLibrary;
	
	/**
	 * ID of Library
	 */
	private Integer idLibrary;
	
	/**
	 * Description of Library
	 */
	private String descrLibrary;
	
	/**
	 * Catalogues of literature 
	 */
	public List<Catalogue> catLibrary;
	
	/**
	 * Librarians' networks
	 */
	private List<LibrarianNetwork> netLIB;
	
	/**
	 * Readers' networks of {@link ReaderNetwork}
	 */
	private List<ReaderNetwork> netREAD;
	
	
//===========================================================================================
//===========================================================================================
	public Library(String nameLibrary)
	{
		this.setNameLibrary(nameLibrary);
		this.catLibrary=new ArrayList<Catalogue>();
		this.setNetLIB(new ArrayList<LibrarianNetwork>());
		this.setNetREAD(new ArrayList<ReaderNetwork>());
	}
	
//==========================================================================================	
//==========================================================================================

	/**
	 * Sets new Catalogue from .catalogue file
	 */
	public void getCatalogueFromFile(String filepath,String filename)
	{		
		BufferedReader rd;
		try {
			rd=new BufferedReader(new FileReader(filepath+filename));
		
			Pattern p;
			Matcher m;
			
			p=Pattern.compile(".catalogue");
			Catalogue cat=new Catalogue();
			cat.setNameCatalogue(p.split(filename)[0]);
			//System.out.println(p.split(filename)[0]);
			
			String s="";
			while ((s=rd.readLine())!=null)
			{														
					System.out.println(s);
										
					String w,t;
					w=getProperty(s, "(\"languageLit\":\")|(\",)");
					if (w.length()>0)
					{
						//System.out.println(w);
						BaseLanguage bas=AbstractLanguage.getLanguage(w);
			
						s=rd.readLine();
						t=getProperty(s, "(\"typeOfLiterature\":\")|(\",)");
						
						if (t.length()>0)
						{
							//System.out.println(t);
							s=rd.readLine();
							w=getProperty(s, "(\"nameLit\":\")|(\",)");
							
							if (w.length()>0)
							{
								//System.out.println(w);
								LITERATURE lt=LITERATURE.valueOf(t.toUpperCase());
								
								switch(lt)
								{
									case BOOK:
									{ 
										Literature lit=bas.createBooks(w);
										lit.setTypeOfLiterature(t);
										s=rd.readLine();
										w=getProperty(s, "(\"amountOfPages\":\")|(\",)");
										System.out.println(w+"   "+Integer.parseInt(w));
										lit.setAmountOfPages(Integer.parseInt(w));
										
										s=rd.readLine();
										w=getProperty(s, "(\"availability\":\")|(\",)");
										//System.out.println(w);
										//lit.setAvailabilityOfLit(Boolean.valueOf(w));
										//this.catLibrary[this.catLibrary.size()-1].
										cat.addLiterature(lit);
										break;
									}
									case JOURNAL:
									{
										Literature lit=bas.createJournals(w);
										lit.setTypeOfLiterature(t);
										s=rd.readLine();
										w=getProperty(s, "(\"amountOfPages\":\")|(\",)");
										System.out.println(w+"   "+Integer.parseInt(w));
										lit.setAmountOfPages(Integer.parseInt(w));
										
										//System.out.println(w);
										s=rd.readLine();
										w=getProperty(s, "(\"availability\":\")|(\",)");
										//System.out.println(w);
										//lit.setAvailabilityOfLit(Boolean.valueOf(w));
										cat.addLiterature(lit);
										break;
									}
									default:{}
								}
							}
						}
					}
					
					//Literature lit;
					}
			rd.close();
			
			this.catLibrary.add(cat);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//rd.close();
		}
		catch (IOException e)
		{
		//	rd.close();
		}	
	}
	/**
	 * Method that returns current Literature's property from line s using regex
	 * @param s Line that contains property
	 * @param reg Rule which helps to get Property
	 * @return property containing in line s
	 */
	public String getProperty(String s,String reg)
	{

		Pattern p=Pattern.compile(reg);
		Matcher m=p.matcher(s);
		try
		{
			if (m.find())
			{
				//System.out.println(p.split(s)[1]);
				return (String)(p.split(s)[1]);
			}
		}
		catch(Exception e)
		{
		}
		return "";
	}
	
	/**
	 * Finds catalogue by name, returns 0 if it's not existing/
	 * @param nameCat Name of catalogue
	 * @return Index in listCatalogue of Library
	 */
	public int findCatalogue(String nameCat)
	{
		return 0;
	}
	
	/**
	 * Sets the initial values of library
	 * @return Library with a set of initial values 
	 */
	public static Library setUpLibrary()
	{
		Locale l=new Locale("ru");
		ResourceBundle resb=ResourceBundle.getBundle("first.localiz.SampleRB",Locale.getDefault());
		
		String path=File.separator+"media"+File.separator+"B32740CCC92117C1"+File.separator+"JavaProjects"+File.separator+"test"+File.separator;
		
		path=LibDialog.findMainPath(path, l);
		
		//LibDialog.checkAdmin(path, l);
		
		File f=new File(path);
				
			System.out.println(Locale.getDefault());
		
			System.out.println(resb.getString("title")+"\n\n");
		
			Library LIB=new Library(resb.getString("Library"));
		
			LIB.getCatalogueFromFile(path,"phythics.catalogue");
			
			System.out.println(LIB.catLibrary.get(0).amountOfText());
			return LIB;
		
	}
//==========================================================================================	
//==========================================================================================
	
	/**
	 * @param netLIB the netLIB to set
	 */
	public void setNetLIB(List<LibrarianNetwork> netLIB) {
		this.netLIB = netLIB;
	}


	/**
	 * @return the netLIB
	 */
	public List<LibrarianNetwork> getNetLIB() {
		return netLIB;
	}


	/**
	 * @param netREAD the netREAD to set
	 */
	public void setNetREAD(List<ReaderNetwork> netREAD) {
		this.netREAD = netREAD;
	}


	/**
	 * @return the netREAD
	 */
	public List<ReaderNetwork> getNetREAD() {
		return netREAD;
	}

	/**
	 * @param nameLibrary the nameLibrary to set
	 */
	public void setNameLibrary(String nameLibrary) {
		this.nameLibrary = nameLibrary;
	}

	/**
	 * @return the nameLibrary
	 */
	public String getNameLibrary() {
		return nameLibrary;
	}

	/**
	 * Sets the ID of Library 
	 * @param idLibrary
	 */
	public void setIdLibrary(Integer idLibrary) {
		this.idLibrary = idLibrary;
	}
	/**
	 * 
	 * @return ID of library
	 */
	public Integer getIdLibrary() {
		return idLibrary;
	}

	/**
	 * Sets description of Library
	 * @param descrLibrary
	 */
	public void setDescrLibrary(String descrLibrary) {
		this.descrLibrary = descrLibrary;
	}

	/**
	 * Returns description of Library
	 * @return String Library.descrLibrary
	 */
	public String getDescrLibrary() {
		return descrLibrary;
	}
	
	

}
